package com.example.my_bookkeeping;

import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.my_bookkeeping.callback.SwitchViewPager;
import com.example.my_bookkeeping.ui.account.AccountFragment;
import com.example.my_bookkeeping.ui.bookkeep.BookkeepFragment;
import com.example.my_bookkeeping.ui.category.CategoryFragment;
import com.example.my_bookkeeping.ui.statistic.StatisticFragment;
import com.example.my_bookkeeping.ui.record.RecordActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements SwitchViewPager {

    private ActivityResultLauncher<Intent> recordLauncher;
    private ViewPager2 viewPager2;
    private BottomNavigationView navView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        navView = findViewById(R.id.bottom_navigation_view);
        viewPager2 = findViewById(R.id.view_pager2_main);
        viewPager2.setAdapter(new ViewPagerAdapter(this));
        viewPager2.setUserInputEnabled(false);

        // 注册启动器
        recordLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> switchViewPager(2)
        );

        // 底部导航栏点击事件
        navView.setOnItemSelectedListener(item -> {
            int id = item.getItemId();
            if (id == R.id.item_statistic) {
                viewPager2.setCurrentItem(0);
            } else if (id == R.id.item_category) {
                viewPager2.setCurrentItem(1);
            } else if (id == R.id.item_record) {
                Intent intent = new Intent(this, RecordActivity.class);
                recordLauncher.launch(intent);
            } else if (id == R.id.item_bookkeep) {
                viewPager2.setCurrentItem(2);
            } else if (id == R.id.item_account) {
                viewPager2.setCurrentItem(3);
            } else {
                return false;
            }
            return true;
        });
    }

    @Override
    public void switchViewPager(int position) {
        if(position < 0 || position > navView.getMenu().size())return;
        viewPager2.setCurrentItem(position);
        if(position > 1)position++;
        navView.getMenu().getItem(position).setChecked(true);
    }

    static class ViewPagerAdapter extends FragmentStateAdapter {
        private final ArrayList<Fragment> fragments = new ArrayList<>();

        public ViewPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
            fragments.add(new StatisticFragment());
            fragments.add(new CategoryFragment());
            fragments.add(new BookkeepFragment());
            fragments.add(new AccountFragment());
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragments.get(position);
        }

        @Override
        public int getItemCount() {
            return fragments.size();
        }
    }
}

